//
//  BrandView.m
//  CarHome
//
//  Created by 张世琦 on 14-9-23.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "BrandView.h"
#import "BrandModel.h"
#import "CarModel.h"
#import "FctView.h"
#import "SpecView.h"

#define CENTER 160

NSString *const brandCell = @"cellId";

@implementation BrandView

- (void)dealloc
{
    [_letterArray release];
    _letterArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        _letterArray = [[NSMutableArray alloc] init];
        [self createButton];
        [self connect:BrandUrl];
        [self createTableView];
        //添加手势
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognizer];
        [panGestureRecognizer release];
    }
    return self;
}

#pragma mark -
#pragma mark 右侧透明button
//按钮创建
- (void)createButton
{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setFrame:CGRectMake(0, 0, 80, self.frame.size.height)];
    [_button setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_button];
}
//点击事件
- (void)buttonAction:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark 滑动手势

-(void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint point = [recognizer translationInView:self];
    float x = self.center.x + point.x;
    _button.backgroundColor = [UIColor clearColor];
    if (x < CENTER) {
        x = self.center.x;
    }
    
    self.center = CGPointMake(x, self.center.y);
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1
                              delay:0.01
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
            if (x > 250) {
                self.center = CGPointMake(400, self.center.y);
            } else {
                self.center = CGPointMake(CENTER, self.center.y);
                _button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
            }
        } completion:^(BOOL finished) {
            
        }];
    }
    [recognizer setTranslation:CGPointZero inView:self];
}

#pragma mark -
#pragma mark 创建TableView

- (void)createTableView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 240, self.frame.size.height - 20)];
    aView .backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    _bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
    _bView.backgroundColor = [UIColor whiteColor];
    _label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 44)];
    _label.text = @"选择品牌";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(190, 0, 50, 44)];
    [button setTitle:@"关闭" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bView addSubview:button];
    [_bView addSubview:_label];
    [aView addSubview:_bView];
    UIButton *all = [UIButton buttonWithType:UIButtonTypeCustom];
    [all setFrame:CGRectMake(0, 0, 320, 40)];
    [all setTitle:@"全部品牌" forState:UIControlStateNormal];
    [all setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [all addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, 240, self.frame.size.height - 69 - 66) style:UITableViewStylePlain];
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:brandCell];
    _table.tableHeaderView = all;
    _table.dataSource = self;
    _table.delegate = self;
    [aView addSubview:_table];
    [self addSubview:aView];
    
}
//点击事件
- (void)allAction:(id)sender
{
    self.block();
}

#pragma mark -
#pragma mark UITableViewDataSource协议
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BrandModel *brand = [_letterArray objectAtIndex:section];
    return [brand.list count];
}
//重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:brandCell forIndexPath:indexPath];
    BrandModel *brand = [_letterArray objectAtIndex:indexPath.section];
    CarModel *car = [brand.list objectAtIndex:indexPath.row];
    [cell.textLabel setText:car.name];
    return cell;
}

//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_letterArray count];
}

//section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    BrandModel *brand = [_letterArray objectAtIndex:section];
    return brand.letter;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 26; i++) {
        NSString *str = [NSString stringWithFormat:@"%c", (65 + i)];
        [arr addObject:str];
    }
    return arr;
}

#pragma mark -
#pragma mark UITableViewDelegate协议
//cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BrandModel *brand = [_letterArray objectAtIndex:indexPath.section];
    CarModel *car = [brand.list objectAtIndex:indexPath.row];
    FctView *aView = [[FctView alloc] initWithFrame:CGRectMake(80, 0, 240, self.frame.size.height + 64 + 49)];
    aView.fctId = car.carId;
    [aView conncetion:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/cars/seriesprice-a2-pm1-v4.2.0-b%ld-t1.html", (long)car.carId]];
    [self addSubview:aView];
    [aView release];
}

#pragma mark -
#pragma mark 网络请求

- (void)connect:(NSString *)url
{
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary *dic = (NSMutableDictionary *)data;
        NSMutableDictionary *result = [dic objectForKey:@"result"];
        NSMutableArray *brandlist = [result objectForKey:@"brandlist"];
        for (NSMutableDictionary *dic in brandlist) {
            BrandModel *brand = [[BrandModel alloc] init];
            brand.letter = [dic objectForKey:@"letter"];
            NSMutableArray *list = [dic objectForKey:@"list"];
            for (NSMutableDictionary *dic in list) {
                CarModel *car = [[CarModel alloc] init];
                car.name = [dic objectForKey:@"name"];
                car.carId = [[dic objectForKey:@"id"]intValue];
                [brand.list addObject:car];
            }
            [_letterArray addObject:brand];
        }
        [_table reloadData];
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
